import 'dart:async';
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:package_info/package_info.dart';

class AcDeviceTool {
  static AcDeviceTool? _instance;

  factory AcDeviceTool() => _instance ??= AcDeviceTool._();

  AcDeviceTool._() {
    _initializeDeviceInfo();
  }
  static final Completer _completer = Completer();

  static Future<void> init() async {
    AcDeviceTool();
    await _completer.future;
  }

  final DeviceInfoPlugin _deviceInfoPlugin = DeviceInfoPlugin();

  late String _foDeviceId;
  late dynamic _foAndroidDeviceInfo;
  late dynamic _foIosDeviceInfo;
  late String _foLocalLanguageCode;
  late PackageInfo _packageInfo;

  Future<void> _initializeDeviceInfo() async {
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await _deviceInfoPlugin.androidInfo;
      _foDeviceId = androidInfo.id;
      _foAndroidDeviceInfo = androidInfo;
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await _deviceInfoPlugin.iosInfo;
      _foDeviceId = iosInfo.identifierForVendor ?? "";
      _foIosDeviceInfo = iosInfo;
    }
    _foLocalLanguageCode = Platform.localeName;
    _packageInfo = await PackageInfo.fromPlatform();
    _completer.complete();
  }

  String get foDeviceId {
    return _foDeviceId;
  }

  dynamic get foAndroidDeviceInfo {
    return _foAndroidDeviceInfo;
  }

  dynamic get foIosDeviceInfo {
    return _foIosDeviceInfo;
  }

  String get foLocalLanguageCode {
    return _foLocalLanguageCode;
  }

  PackageInfo get packageInfo {
    return _packageInfo;
  }
}
